package com.atmilan.quartz.job;

import java.util.Date;
import java.util.Map;

import com.atmilan.quartz.service.IStoreProductService;
import com.atmilan.quartz.service.impl.StoreProductServiceImpl;
import com.atmilan.quartz.utils.XxlJobParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.constant.enums.SyncStatusEnum;
import com.atmilan.quartz.model.entity.crmeb.SystemConfig;
import com.atmilan.quartz.service.IJkySyncService;
import com.atmilan.quartz.service.ISystemConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;


/**
 * 吉客云商品同步和推送
 * @Description: 
 * @author daigb
 * @date 2021-06-28
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class JkySyncTask {
	@Autowired
    BaseTask baseTask;
    @Autowired
    IJkySyncService jkySyncService;
    @Autowired
    ISystemConfigService systemConfigService;

    @Autowired
    IStoreProductService storeProductService;

    /**
     * 每30分钟同步吉客云商品源
     * @Description:  
     * @author daigb 
     * @param 
     * @return void
     * @throws
     */
    @XxlJob("jkySyncTask")
//    @Scheduled(fixedDelay = 1000 * 60 * 60 * 60 )
    public void syncTask() {
        try {
            SystemConfig systemConfig = systemConfigService.getOne(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getName, Constants.JKY_SYNC_STATUS).last("LIMIT 1"));
            SyncStatusEnum jkySyncStatus = SyncStatusEnum.valueOf(systemConfig.getFormId());
            long timeDiff = new Date().getTime() - systemConfig.getUpdateTime().getTime();

            log.warn("--------------- 开始计划同步吉客云商品, 同步状态为: {} ---------------", jkySyncStatus);
            // 24小时内状态还未变更, 直接重新处理
            if (SyncStatusEnum.WAIT == jkySyncStatus && 24 < (timeDiff / 1000 / 60 / 60)) {
                log.warn("--------------- 吉客云商品同步过程超过24小时 ---------------");
                jkySyncStatus = SyncStatusEnum.PUSH;
            }
            // 有同步任务时不进行处理
            if (SyncStatusEnum.WAIT == jkySyncStatus) {
                log.warn("--------------- 吉客云任务未结束, 新任务不执行 ---------------");
                return;
            }
            // 更新任务状态为同步中
            baseTask.updateSyscStatus(Constants.JKY_SYNC_STATUS, SyncStatusEnum.WAIT, systemConfig);
            // 下发完成, 需要同步
            if (SyncStatusEnum.PUSH == jkySyncStatus) {
                Map<String, String> params = XxlJobParamUtil.getParams();
                Integer intervalOfDays = XxlJobParamUtil.getInteger(params, "intervalOfDays", 3);
                boolean result = jkySyncService.syncProduct(intervalOfDays);
                if (result) {
                	baseTask.updateSyscStatus(Constants.JKY_SYNC_STATUS, SyncStatusEnum.SYNC, systemConfig);
                    log.warn("--------------- 吉客云商品同步完成!!! ---------------");
                }
                // 同步完成, 需要下发
            } else if (SyncStatusEnum.SYNC == jkySyncStatus) {
                boolean result = jkySyncService.pushProduct();
                if (result) {
                	baseTask.updateSyscStatus(Constants.JKY_SYNC_STATUS, SyncStatusEnum.PUSH, systemConfig);
                    log.warn("--------------- 吉客云商品下发完成!!! ---------------");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 每12小时推送吉客云商品源
     * @Description:  
     * @author daigb 
     * @param 
     * @return void
     * @throws
     */
    @XxlJob("jkyPushcTask")
//    @Scheduled(fixedDelay = 1000 * 60 * 60 * 60 )
    public void pushTask() {
        try {
            jkySyncService.remotePushProduct();
            log.warn("--------------- 吉客云商品推送完成!!! ---------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public void testFanyi() {
        try {

            storeProductService.testFanyi();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
